package org.gdkm.el;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.gdkm.el.elt.Str;

/**
 * 界面层类. </br> 
 * 用于减低的程序的耦合性. </br> 
 * 日期: 2012-10-13 </br>
 * <p>
 * 
 * @author Arylo<<b>arylo.open@gmail.com</b>>
 * @version 1.0
 */
public class UiThread {
	private static CheckSocket checkSocket = null;
	private static CheckLogin checkLogin = null;
	private static NoticeDialog noticeDialog = null;
	private static JFrame jf = null;

	/**
	 * 构建一个UiThread对象.
	 */
	public UiThread() {
		checkSocket = new CheckSocket();
		checkLogin = new CheckLogin();
		noticeDialog = new NoticeDialog();
		Id.setId("Panel_Socket", checkSocket);
		Id.setId("Panel_Login", checkLogin);
		Id.setId("Panel_Notice", noticeDialog);
	}

	private static void change(JFrame jFrame) {
		if (jf != null && jf.isVisible() == true) {
			jf.setVisible(false);
		}
		jf = null;
		jf = jFrame;
		jf.setVisible(true);
	}

	/**
	 * 显示检查界面.
	 */
	public static void changeCheck() {
		change(checkSocket);
	}

	/**
	 * 显示登录界面.
	 */
	public static void changeLogin() {
		change(checkLogin);
	}
}

/**
 * 登录界面类. </br> 
 * 用于减低的程序的耦合性. </br> 
 * 日期: 2012-10-13 
 * <p>
 * 
 * @author Arylo<<b>arylo.open@gmail.com</b>>
 * @version 1.0
 */
class CheckLogin extends JFrameBase {
	private static JPanel panel_logo = null;
	private static JPanel panel_id = null;
	private static JPanel panel_pwd = null;
	private static JPanel panel_button = null;
	private static JLabel label_logo = null;
	private static JLabel label_id = null;
	private static JLabel label_pwd = null;
	private static JTextField text_id = null;
	private static JTextField text_pwd = null;
	private static JButton button_login = null;
	private static JButton button_cancel = null;

	/**
	 * 构建一个CheckLogin 对象.
	 */
	public CheckLogin() {
		super();
		this.setLayout(new GridLayout(4, 1, 10, 5));
		panel_logo = new JPanel();
		panel_logo.setLayout(new FlowLayout(FlowLayout.CENTER));
		panel_id = new JPanel();
		panel_id.setLayout(new FlowLayout(FlowLayout.CENTER));
		panel_pwd = new JPanel();
		panel_pwd.setLayout(new FlowLayout(FlowLayout.CENTER));
		panel_button = new JPanel();
		panel_button.setLayout(new FlowLayout(FlowLayout.CENTER));
		label_logo = new JLabel();
		label_logo.setText(APP_OWNER.substring(APP_OWNER.indexOf("-") + 1));
		label_logo.setFont(new Font(null, 1, 20));
		label_id = new JLabel();
		label_id.setText("  ID:  ");
		label_pwd = new JLabel();
		label_pwd.setText("Pwd:");
		text_id = new JTextField();
		text_id.setColumns(16);
		text_pwd = new JTextField();
		text_pwd.setColumns(16);
		button_login = new JButton();
		button_login.setText("Login");
		button_cancel = new JButton();
		button_cancel.setText("Cancel");

		Id.setId("CheckLogin_id", text_id);
		Id.setId("CheckLogin", text_pwd);
		Id.setId("CheckLogin_login", button_login);
		Id.setId("CheckLogin_cancel", button_cancel);

		panel_logo.add(label_logo);
		panel_id.add(label_id);
		panel_id.add(text_id);
		panel_pwd.add(label_pwd);
		panel_pwd.add(text_pwd);
		panel_button.add(button_login);
		panel_button.add(button_cancel);
		this.add(panel_logo);
		this.add(panel_id);
		this.add(panel_pwd);
		this.add(panel_button);
		this.setTitle("Login" + APP_OWNER);
		this.setSizeAndCenter(280, 210);
	}
}

/**
 * 检查Socket界面类. </br> 
 * 用于减低的程序的耦合性. </br> 
 * 日期: 2012-10-13 </br>
 * <p>
 * 
 * @author Arylo<<b>arylo.open@gmail.com</b>>
 * @version 1.0
 */
class CheckSocket extends JFrameBase {
	private JPanel jp1 = null;
	private JPanel jp2 = null;
	private JLabel jl = null;
	private JButton re = null;
	private JButton more = null;

	/**
	 * 构建一个CheckSocket 对象.
	 */
	public CheckSocket() {
		super();
		jp1 = new JPanel();
		jp1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 100));
		jp2 = new JPanel();
		jl = new JLabel();
		re = new JButton();
		more = new JButton();

		Id.setId("CheckSocket_text", jl);
		Id.setId("CheckSocket_button", re);
		Id.setId("CheckSocket_more", more);

		jp1.add(jl, SwingConstants.CENTER);
		jp2.add(more);
		jp2.add(re);
		this.add(jp1, BorderLayout.CENTER);
		this.add(jp2, BorderLayout.SOUTH);
		this.setTitle("Checking" + APP_OWNER);
		this.setSizeAndCenter(280, 210);
	}
}

/**
 * 提示界面类. </br> 
 * 日期: 2012-10-13 </br>
 * <p>
 * 
 * @author Arylo<<b>arylo.open@gmail.com</b>>
 * @version 1.0
 */
class NoticeDialog extends JFrameBase {
	private JPanel panel_text = null;
	private JPanel panel_button = null;
	private JLabel text_notice = null;
	private JButton button_sure = null;

	/**
	 * 构建一个NoticeDialog 对象.
	 */
	public NoticeDialog() {
		this.setLayout(new GridLayout(2, 1, 10, 5));
		panel_text = new JPanel();
		panel_text.setLayout(new FlowLayout(FlowLayout.CENTER));
		panel_button = new JPanel();
		panel_button.setLayout(new FlowLayout(FlowLayout.CENTER));
		text_notice = new JLabel();
		button_sure = new JButton();
		button_sure.setText("OK");

		panel_text.add(text_notice);
		panel_button.add(button_sure);
		this.add(panel_text);
		this.add(panel_button);
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		this.setTitle("Warning" + APP_OWNER);
		this.setSizeAndCenter(180, 120);
	}

	/**
	 * 用于显示提示窗体的方法.
	 * 
	 * @param text
	 *            提醒文字.
	 * @param b
	 *            是否显示窗体.
	 */
	public void setVisible(String text, boolean b) {
		text_notice.setText(text);
		this.setVisible(b);
	}
}

/**
 * 基础界面类. </br> 
 * 用于提高代码的重用性. </br> 
 * 日期:2012-10-13
 * <p>
 * 
 * @author Arylo<<b>arylo.open@gmail.com</b>>
 * @version 1.0
 */
class JFrameBase extends JFrame implements Str {
	/**
	 * 构建一个JFrameBase 对象.
	 */
	public JFrameBase() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	/**
	 * 设置窗体的大小并居中.
	 * 
	 * @param width
	 *            窗体的宽度.
	 * @param height
	 *            窗体的高度.
	 */
	protected void setSizeAndCenter(int width, int height) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(width, height);
		this.setLocation(screenSize.width / 2 - width / 2, screenSize.height
				/ 2 - height / 2);
	}
}